package com.itlong.cloud.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.foreign.WorkWechatTokenResponse;
import com.itlong.cloud.properties.WorkWechatProperties;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.service.WorkWechat;
import com.itlong.cloud.utils.tool.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class WorkWechatImpl implements WorkWechat {

    private static final Logger LOG = LoggerFactory.getLogger(WorkWechatImpl.class);

    @Resource
    WorkWechatProperties workWechatProperties;

    @Autowired
    private RedisService redisService;

    @Autowired
    RestTemplate restTemplate;

    @Override
    public WorkWechatTokenResponse getToken() {
        HashMap<String, Object> p = new HashMap<>();
        p.put("corpid", workWechatProperties.getCorpId());
        p.put("corpsecret", workWechatProperties.getCorpSecret());
        String response = HttpUtil.sendGetRequestWithHashMap(restTemplate, workWechatProperties.getDomain() + "/cgi-bin/gettoken", p, null, String.class);
        LOG.info("【WorkWechatTokenResponse】{}", response);
        WorkWechatTokenResponse workWechatTokenResponse = JSONObject.parseObject(response, WorkWechatTokenResponse.class);
        if (workWechatTokenResponse.getErrcode().equals(0)) {
            return workWechatTokenResponse;
        }
        return null;
    }

    @Override
    public String getTokenByCache() {
        String key = String.format("wechat_token_%s", workWechatProperties.getCorpId());
        String token = redisService.get(key);
        if (token != null && !token.equals("")) {
            return token;
        }
        WorkWechatTokenResponse workWechatTokenResponse = getToken();
        if (workWechatTokenResponse != null) {
            token = workWechatTokenResponse.getAccess_token();
            redisService.set(key, workWechatTokenResponse.getExpires_in() - 300, token);
            return token;
        }
        return "";
    }

    @Override
    public String sendText(String content) {
        //关闭
        if (workWechatProperties.getOpen() == null || !workWechatProperties.getOpen().equals(1)) {
            return "{\"errcode\":9001,\"errmsg\":\"open is not set 1\"}";
        }
        String token = getTokenByCache();
        if (token.equals("")) {
            return "{\"errcode\":9002,\"errmsg\":\"get token error\"}";
        }
        HashMap<String, Object> text = new HashMap<>();
        text.put("content", content);
        //
        HashMap<String, Object> p = new HashMap<>();
        p.put("agentid", workWechatProperties.getAgentId());
        p.put("msgtype", "text");
        p.put("text", text);
        p.put("touser", "@all");
        //
        HashMap<String, Object> h = new HashMap<>();
        return HttpUtil.sendPostRequestWithHashMap(restTemplate, workWechatProperties.getDomain() + "/cgi-bin/message/send?access_token=" + token, p, h, String.class, true);
    }

}